<?php
$sourcePage = $_GET['sourcePage'];
//$filtro = ' WHERE stock = 5';
$filtro = $_GET['filtro'];

// load error handling script and the Grid class
require_once('error_handler.php');
require_once('gridGenerica.class.php');


$action = 'load';
if(isset($_GET['action']))	
	$action = $_GET['action'];

if($action == 'load')
{

	$page = $_POST['page']; // get the requested page
	$limit = $_POST['rows']; // get how many rows we want to have into the grid
	$sidx = $_POST['sidx']; // get index row - i.e. user click to sort
	$sord = $_POST['sord']; // get the direction
	$grid = new Grid($page,$limit,$sidx,$sord);
	$response->page = $page;
	$response->total = $grid->getTotalPages($filtro);
	$response->records = $grid->getTotalItemsCount($filtro);
	$currentPageItems = $grid->getCurrentPageItems($filtro);

		
	for($i=0;$i<count($currentPageItems);$i++) {
		$response->rows[$i]['id']=$currentPageItems[$i][$colsArray[0]];
		$array = array();
		foreach ($colsArray as $col) { 
			array_push($array, $currentPageItems[$i][$col]);
		}		
		$response->rows[$i]['cell']=$array;  
	} 
	echo json_encode($response);
}
elseif ($action == 'save')
{
	$col1 = $_POST[COL1];
	$col2 = $_POST[COL2];
	$col3 = $_POST[COL3];
	$col4 = $_POST[COL4];
	$col5 = $_POST[COL5];
	
	$grid = new Grid();
	echo $grid->updateItem($col1,$col2,$col3,$col4,$col5);
}
?>
